package com.wtf.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author tengfei wang
 * @version 1.0
 * @date Created in 2020/11/2
 */
@Service
@Slf4j
@RefreshScope
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Override
    public boolean sendEmail(String email, Integer code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("wtf_182@163.com");
            message.setTo(email);
            message.setSubject("验证码");
            message.setText("您的验证码为" + code + "，有效期为十分钟");
            javaMailSender.send(message);
            log.info("send code {} to email {} success", code, email);
        } catch (MailException e) {
            log.error("发送邮件异常：{}", e.getMessage());
            return false;
        }
        return true;
    }
}
